package com.baosight.sczx.zm.ch.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHGrubReport;
import com.baosight.sczx.zm.ch.domain.ZMCHMineReport;

/**
 * 掘进日上报
 * <p>Title: ServiceZMCP95.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-9-6 上午06:30:28  严洪浩  Create
 *</pre>
 */
public class ServiceZMCH10 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午06:31:00 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的 
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午06:31:28 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {			
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    String departmentId = (String)UserSession.getInSessionProperty("orgCode"); 

		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);
		StaticTools.setQueryParameters(inInfo, "fdReportDepartmentId", departmentId, true);
		EiInfo outInfo = super.query(inInfo, "ZMCHGrubReport.query", new ZMCHGrubReport());
		

		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));//班次
		outInfo.addBlock(Engineering.getEngineeringForJJ("cmes.cp.engineeringInqu", "1", "", "1", true,fdMiningId,departmentId));
		
		outInfo.addBlock(StaticBlocks.getStauts4());//状态
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdMiningId", fdMiningId);//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		outInfo.set("userId", UserSession.getUserId());
		outInfo.set("userName", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("departmentId", departmentId);
		outInfo.set("departmentName", UserSession.getInSessionProperty("orgName"));
		return outInfo;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:05:43 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo();
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;		
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			boolean validate = validate(inInfo, _dom, "ZMCHGrubReport.validate", params);
			if (validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			else {
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_GRUBREPORT"));//主键
				
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
//				_dom.setFdCurrentDate(StaticTools.getDate());// 当前日期
				_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());// 汇报部门id
				_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
				_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
				_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
				
				_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
				this.dao.insert("ZMCHGrubReport.insert",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 	
		
		}
		
	
	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP95</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:09:01 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = validate(inInfo, _dom, "ZMCHGrubReport.validate", params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			else {
				this.dao.update("ZMCHGrubReport.update",_dom);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	

		}
	

	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-6下午03:58:35 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCHGrubReport.delete", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   

	}
	
	/**
	 * 上报
	 * <p>Function: modifyStop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-13下午08:12:20 严洪浩
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{	
		EiInfo out=new EiInfo(); 
		ZMCHGrubReport _dom = new ZMCHGrubReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdStatus("上报");
			dao.update("ZMCHGrubReport.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
	

	/**
	 * 验证上报是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-6上午09:04:59 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, ZMCHGrubReport _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdWorknumId = _dom.getFdWorknumId();
		String fdMiningId = _dom.getFdMiningId();
		String fdCurrentDate = _dom.getFdCurrentDate();
		
		params.put("fdWorknumId", fdWorknumId);
		params.put("fdMiningId", fdMiningId);
		params.put("fdCurrentDate", fdCurrentDate);
		List result = dao.query("ZMCHGrubReport.validate", params);

		if (result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "掘进进度汇报每单位每天每班只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
	
	


}


			

